package org.example.musicproject.service.impl;

import org.example.musicproject.dao.CommentAddsongsDao;
import org.example.musicproject.dao.CommentsDao;
import org.example.musicproject.dao.SongDao;
import org.example.musicproject.dao.impl.CommentAddsongsDaoImpl;
import org.example.musicproject.dao.impl.CommentsDaoImpl;
import org.example.musicproject.dao.impl.SongDaoImpl;
import org.example.musicproject.entity.Comments;
import org.example.musicproject.entity.Song;
import org.example.musicproject.service.SongService;
import org.example.musicproject.util.DButil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

/**
 * 创建人:黄庭萍
 * 创建时间: 2024/12/16 19:43
 * 创建类型: SongServiceImpl
 */
public class SongServiceImpl implements SongService {
    private final SongDao songDao = new SongDaoImpl();
    private final CommentAddsongsDao commentAddsongsDao = new CommentAddsongsDaoImpl();

    @Override
    public List<Song> findAll() {
        return songDao.findAll();
    }

    @Override
    public Song byId(int soid) {
        return songDao.byId(soid);
    }

    @Override
    public long add(Song song, int[] cids) {
        return executeTransaction(conn -> {
            long soid = songDao.add(conn, song);
            if (soid > 0 && cids != null) {
                for (int cid : cids) {
                    commentAddsongsDao.add(conn, cid, (int) soid);
                }
            }
            return soid;
        });
    }

    @Override
    public int update(Song song) {
        return  songDao.update(song);
    }

    @Override
    public int isVips(int soid, boolean isVip) {
        return songDao.isVips(soid, isVip);
    }

    @Override
    public List<Song> bykey(String key) {
        return songDao.bykey(key);
    }

    @Override
    public List<Song> number(int num) {
        return songDao.number(num);
    }

    /**
     * 执行数据库事务，并确保事务提交或回滚，最后关闭连接。
     *
     * @param transaction 事务逻辑
     * @param <T>         返回类型
     * @return 操作结果
     */
    private <T> T executeTransaction(Transaction<T> transaction) {
        Connection conn = null;
        try {
            conn = DButil.getConn();
            conn.setAutoCommit(false);
            T result = transaction.execute(conn);
            conn.commit();
            return result;
        } catch (SQLException e) {
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException rollbackEx) {
                    throw new RuntimeException("事务回滚失败：" + rollbackEx.getMessage(), rollbackEx);
                }
            }
            throw new RuntimeException("数据库操作失败：" + e.getMessage(), e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException closeEx) {
                    throw new RuntimeException("关闭数据库连接失败：" + closeEx.getMessage(), closeEx);
                }
            }
        }
    }

    // 定义一个事务接口
    @FunctionalInterface
    interface Transaction<T> {
        T execute(Connection conn) throws SQLException;
    }
}